﻿using System;
using System.Text.RegularExpressions;
using System.Web;

public class UrlRewriter : IHttpHandler
{
	public bool IsReusable
	{
		get { return false; }
	}

	public void ProcessRequest(HttpContext context)
	{
		string url = context.Request.Url.PathAndQuery;
		try
		{
			Match m = new Regex("/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})\\.aspx", RegexOptions.IgnoreCase).Match(url, url.LastIndexOf("/"));
			if (m.Success)
			{
				context.Server.Execute("~/products.aspx?guid=" + m.Groups[1].Value);
			} else
			{
				throw new Exception();
			}
		} catch
		{
			context.Response.Redirect(url, true);
		}
	}
}
public class ImageGetter : IHttpHandler
{
	public bool IsReusable
	{
		get { return false; }
	}

	public void ProcessRequest(HttpContext context)
	{
		string url = context.Request.Url.PathAndQuery;
		Match m = new Regex("/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})\\.ashx", RegexOptions.IgnoreCase).Match(url, url.LastIndexOf("/"));

		context.Response.Clear();
		context.Response.ContentType = "image/jpeg";

		string path = context.Server.MapPath("~/App_Data/Products/" + m.Groups[1].Value);

		if (System.IO.File.Exists(path))
		{
			context.Response.WriteFile(path);
		} else
		{
			context.Response.WriteFile(context.Server.MapPath("~/images/none.gif"));
		}
		context.Response.End();
	}
}